package com.example.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Configuration
public class DruidConfig {

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean<StatViewServlet> statViewServletServletRegistration = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String, String> initParameters = new HashMap<>(8);
        //druid web登录验证
        initParameters.put("loginUsername", "admin");
        initParameters.put("loginPassword", "123");
        //允许访问
        initParameters.put("allow", "");
        //禁止访问
        //initParameters.put("guest", "127.0.0.1");
        statViewServletServletRegistration.setInitParameters(initParameters);
        return statViewServletServletRegistration;
    }

    @Bean
    public FilterRegistrationBean druidFilter() {
        FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistration = new FilterRegistrationBean<>(new WebStatFilter());
        webStatFilterFilterRegistration.addUrlPatterns("/*");
        webStatFilterFilterRegistration.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,*.html");
        return webStatFilterFilterRegistration;
    }
}
